/*
 * Copyright (c) 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.libermundi.tapestry.elfinder.pages;

import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.corelib.components.Any;
import org.apache.tapestry5.internal.EmptyEventContext;
import org.apache.tapestry5.internal.services.ArrayEventContext;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.TypeCoercer;
import org.apache.tapestry5.services.ComponentEventLinkEncoder;
import org.apache.tapestry5.services.ComponentEventRequestParameters;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.got5.tapestry5.jquery.ImportJQueryUI;

/**
 * @author Martin Papy
 *
 */
@ImportJQueryUI(value={
		"jquery.ui.mouse",
		"jquery.ui.selectable",
		"jquery.ui.draggable",
		"jquery.ui.droppable",
		"jquery.ui.resizable",
		"jquery.ui.dialog",
		"jquery.ui.slider"
},theme="${elfinder.library}/css/smoothness.css")
@Import(stylesheet={
			"${elfinder.library}/css/elfinder.min.css",
			"${elfinder.library}/css/theme.css"
		},
		library={
			"${elfinder.library}/js/elfinder.min.js",
			"${elfinder.library}/js/i18n/elfinder.js",
			"${elfinder.library}/js/elfinder.ckeditor.js"
})
public class ElFinder {
	@Component
	private Any _elFinder;
	
	@Inject
	private JavaScriptSupport _jsSupport;
	
	@Inject
	private ComponentEventLinkEncoder _componentEventLinkEncoder;
	
    @Inject
    private TypeCoercer _typeCoercer;
    
	@AfterRender
	public void afterRender(){
		StringBuffer elFinderInit = new StringBuffer();
		elFinderInit.append("$().ready(function() {")
			.append("var funcNum = getUrlParam('CKEditorFuncNum');")
			.append("var elf = $('#%s').elfinder({")
			.append("url : '%s',")
			.append("getFileCallback : function(file) {")
			.append("window.opener.CKEDITOR.tools.callFunction(funcNum, file.url);")
			.append("window.close();")
			.append("},")
			.append("resizable: false")
			.append("}).elfinder('instance');")
			.append("});");
		
        ComponentEventRequestParameters parameters = new ComponentEventRequestParameters("elfinder/ElFinderConnector", "elfinder/ElFinderConnector", "",
        		ElFinderConnector.ELFINDER_EVENT, new ArrayEventContext(_typeCoercer), new EmptyEventContext());
                    
		_jsSupport.addScript(elFinderInit.toString(), _elFinder.getClientId(),_componentEventLinkEncoder.createComponentEventLink(parameters, false));
	}
	

}
